//
//  PTPairBluetooth.swift
//  PTWatch Watch App
//
//  Created by sc-chen on 2023/9/20.
//

import SwiftUI

struct PTPairBluetooth: View {
    @State private var uuid = ""
    
    @ObservedObject var bluetoothManager = PTBluetoothManager()
    
    @State private var isShowingHUD = false
    @State private var detailTxt = "不支持开发功能"
    
    var body: some View {
        ZStack {
            VStack {
                HStack {
                    Text("输入蓝牙mac地址")
                        .padding()
                        .padding(.top,10)
                    Spacer()
                }
                TextField("请输入", text: $uuid)
                    .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color.gray, lineWidth: 1)
                    )
                    .submitLabel(.done)
                Button {
                    if bluetoothManager.isBluetoothOn {
                        if !uuid.isEmpty {
                            bluetoothManager.startPaired(uid: uuid)
                        }else{
                            showHud(text: "请输入蓝牙mac地址")
                        }
                    }else{
                        showHud(text: "请打开蓝牙权限!")
                    }
                } label: {
                    Text("去配对")
                        .padding(EdgeInsets(top: 5, leading: 20, bottom: 5, trailing: 20))
                        .background(Color.blue)
                        .foregroundColor(Color.white)
                        .cornerRadius(5)
                }
                .buttonStyle(.borderless)
                .padding(.top,20)
                
                Button {
                    bluetoothManager.endPaired()
                } label: {
                    Text("断开连接")
                        .padding(EdgeInsets(top: 5, leading: 20, bottom: 5, trailing: 20))
                        .background(Color.blue)
                        .foregroundColor(Color.white)
                        .cornerRadius(5)
                }
                .buttonStyle(.borderless)
                .padding()
                
                if !bluetoothManager.status.isEmpty {
                    Text(bluetoothManager.status)
                        .padding(EdgeInsets(top: 40, leading: 30, bottom: 10, trailing: 30))
                        .background(Color.white)
                        .foregroundColor(.black)
                }
                Spacer()
            }
            PTProgressHud(isPresented: $isShowingHUD, text: $detailTxt)
        }.onAppear(){
            DispatchQueue.main.asyncAfter(deadline: .now()+1){
                bluetoothManager.startScanning()
            }
        }
    }
    
    private func showHud(text: String) {
        isShowingHUD = true
        detailTxt = text
        DispatchQueue.main.asyncAfter(deadline: .now()+2){
            isShowingHUD = false
        }
    }
}

struct PTPairBluetooth_Previews: PreviewProvider {
    static var previews: some View {
        PTPairBluetooth()
    }
}
